#!/bin/bash
# Installation script for the nettop
# v1.0.0
#########################################


#########################################

#########################################
if [ $USER != "root" ]; then
 echo "This script must be run as root"
 exit -1
fi;

#Update
if [ -z "`grep Scriptaddedrepos /etc/apt/sources.list`" ]; then
 cp /etc/apt/sources.list /etc/apt/sources.list.old
 echo "#Scriptaddedrepos
" > /etc/apt/sources.list
 cat /etc/apt/sources.list.old | sed 's/#\s*deb/deb/g' >> /etc/apt/sources.list

 #Add medibuntu repos
 wget --output-document=/etc/apt/sources.list.d/medibuntu.list http://www.medibuntu.org/sources.list.d/$(lsb_release -cs).list &&  apt-get --quiet update && apt-get --yes --quiet --allow-unauthenticated install medibuntu-keyring && apt-get --quiet update

else
 echo "Sources.list already modified"

fi;

apt-get update
apt-get -y upgrade

#Make net to connect at startup and get the correct IP
if [ -z "`grep Scriptaddedifs /etc/network/interfaces`" ]; then
 
 sudo apt-get -y remove --purge network-manager

 /etc/init.d/networking stop

 cp /etc/network/interfaces /etc/network/interfaces.old

    echo " #Scriptaddedifs
   auto eth0
   iface eth0 inet static
   address 192.168.1.2
   netmask 255.255.255.0
   gateway 192.168.1.1
   pre-up sleep 20
 
   auto lo
   iface lo inet loopback" > /etc/network/interfaces

 cp /etc/resolv.conf /etc/resolv.conf.old
 echo "#Scriptaddedifs
nameserver 8.8.8.8
nameserver 8.8.4.4" > /etc/resolv.conf
 
 /etc/init.d/networking restart

 sleep 100
fi;

#Install basic packages
apt-get -y install expect
apt-get -y install samba
apt-get -y install ntfsprogs ntfs-3g
apt-get -y install openssh-server
apt-get -y install cups

#Change the passwords
echo 'spawn passwd root
  expect "UNIX:"
  send "n1n10c4b3z0n\r"
  expect "UNIX:"
  send "n1n10c4b3z0n\r"
  expect eof' > /tmp/expect.cmd
expect -f /tmp/expect.cmd
rm /tmp/expect.cmd

echo 'spawn passwd administrador
  expect "UNIX:"
  send "4dm1n1str4d0r\r"
  expect "UNIX:"
  send "4dm1n1str4d0r\r"
  expect eof' > /tmp/expect.cmd
expect -f /tmp/expect.cmd
rm /tmp/expect.cmd

#Add users
#Change fstab
if [ -z "`grep emulatron /etc/fstab`" ]; then
 cp /etc/fstab /etc/fstab.old
 mkdir /media/emulatron
 mkdir /media/nettop
 chmod 777 /media/emulatron /media/nettop 
 echo "/dev/disk/by-label/emulatron	/media/emulatron	ntfs-3g	defaults,auto,exec,rw,uid=1000,gid=1000,umask=000	0	0" >> /etc/fstab
 echo "/dev/disk/by-label/nettop	/media/nettop	ntfs-3g	defaults,auto,exec,rw,uid=1000,gid=1000,umask=000	0	0" >> /etc/fstab
 mount -a

else
 echo "Fstab already modified"

fi;

#TODO:Declare samba units
if [ -z "`grep ScriptAddedUnits /etc/samba/smb.conf`" ]; then
 cp /etc/samba/smb.conf /etc/samba/smb.conf.old

 /etc/init.d/samba stop 
 echo "
#ScriptAddedUnits
[emulatron]
   path = /media/emulatron
   guest ok = yes
   guest only = yes
   read only = no
   case sensitive = no
   strict locking = no
   browseable = yes
   writable = yes
   available = yes

[nettop]
   path = /media/nettop
   guest ok = yes
   guest only = yes
   read only = no
   case sensitive = no
   strict locking = no
   browseable = yes
   writable = yes
   available = yes
" >> /etc/samba/smb.conf
 /etc/init.d/samba start

else
 echo "samba already modified"

fi;


exit

#TODO:Install media server software
# http://lifehacker.com/5391308/build-a-silent-standalone-xbmc-media-center-on-the-cheap
# http://lifehacker.com/5406563/build-a-cheap-but-powerful-boxee-media-center


#TODO:Install mldonkey
apt-get -y install mldonkey-server
/etc/init.d/mldonkey-server stop
cp install_files/mldonkey-server /etc/default/mldonkey-server
mv /var/lib/mldonkey /home/debuti/.mldonkey
cp install_files/mldonkey-server_init /etc/init.d/mldonkey-server
rm -rf .mldonkey/incoming
ln -s /media/sdb1/DOWNLOAD/INCOMING .mldonkey/incoming
rm .mldonkey/users.ini
cp install_files/users.ini .mldonkey/users.ini
echo LA_PASS_DE_admin_ES_t3n4z1t4s
rm -rf .mldonkey/temp
ln -s /media/sdb1/DOWNLOAD/temp .mldonkey/temp
cp install_files/downloads.ini .mldonkey/downloads.ini
/etc/init.d/mldonkey-server start


#TODO:Tomcat + guacamole
apt-get -y install tomcat6
wget http://sourceforge.net/projects/guacamole/files/guacamole-0.14.tar.gz/download
tar xzvf guacamole-0.14.tar.gz
rm guacamole-0.14.tar.gz
mkdir /var/lib/guacamole
mv guacamole-0.14/guacamole.war /var/lib/guacamole/.
#TODO:Cambiar automaticamente los valores de esto
mv guacamole-0.14/guacamole-users.xml /var/lib/tomcat6/conf/.
mv guacamole-0.14/guacamole.xml /var/lib/tomcat6/conf/Catalina/localhost/.
rm -rf  guacamole-0.14
/etc/init.d/tomcat6 restart


#TODO:mp3rip + dvdrip
#mp3gain
apt-get -y install mp3gain

#subsonic en el 8888
apt-get -y install subsonic

#pacpl
apt-get -y install pacpl

#mediubuntu packages
sudo wget http://www.medibuntu.org/sources.list.d/hardy.list -O /etc/apt/sources.list.
d/medibuntu.list
sudo apt-get update
sudo apt-get --force-yes -y install medibuntu-keyring
sudo apt-get update
sudo apt-get -y install ubuntu-restricted-extras
sudo apt-get -y install build-essential
sudo apt-get -y install lame flac vorbis-tools wavpack mplayer ffmpeg speex toolame fa
ad faac sox


#ogmrip
apt-get -y install ogmrip

#shrip
wget http://heanet.dl.sourceforge.net/sourceforge/ogmrip/shrip-0.4.0.tar.gz
tar xzf shrip-0.4.0.tar.gz
rm shrip-0.4.0.tar.gz
cd shrip-0.4.0
sudo apt-get install pkg-config libglib2.0-dev
./configure
make
sudo make install
cd ..
cd ..
rm -rf temp






#Configure .bashrc to make some tasks at init
if [ ! -e "/etc/init.d/startupConfigScript" ]; then
 echo '!/bin/bash
 #Startup actions for all users
 
 ## Update
 apt-get update
 apt-get upgrade

 ## Update if necessary
 #wget http://192.168.1.100/update.sh /tmp/update.sh
 #/tmp/update.sh
'> /etc/init.d/startupConfigScript
 chmod +x /etc/init.d/startupConfigScript
 update-rc.d startupConfigScript defaults
fi;

#Install needed applications
apt-get -y install flashplugin-nonfree
apt-get -y install vlc mozilla-plugin-vlc
apt-get -y remove totem.* totem-common.* .*brasero.*
apt-get -y install unrar rar p7zip p7zip-full p7zip-rar
apt-get -y install amarok
apt-get -y install ntp
apt-get -y install sun-java6-jdk

#Install admin applications
apt-get -y install tkdiff gparted mercurial vim nmap ncdu wget screen

#Enable Vnc
gconftool-2 -s -t bool /desktop/gnome/remote_access/enabled true
gconftool-2 -s -t bool /desktop/gnome/remote_access/prompt_enabled false
gconftool-2 -s -t string /desktop/gnome/remote_access/vnc_password n1n10c4b3z0n

 #Install guacamole
 apt-get -y install tomcat6


#Install and configure torrent manager
#Dropbox
